import away3d.audio.*;
import away3d.cameras.*;
import away3d.cameras.lenses.*;
import away3d.containers.*;
import away3d.core.base.*;
import away3d.core.clip.*;
import away3d.core.math.*;
import away3d.events.*;
import away3d.lights.*;
import away3d.loaders.*;
import away3d.materials.*;
import away3d.primitives.*;
import away3d.sprites.*;

import com.powerfl.frm.taf.*;

import flash.display.*;
import flash.media.*;
import flash.text.*;
import flash.utils.*;

final public function addIni( $key:String, $path:String, ...$param ):void{
	var param:TAFparam;
	param = $paramGet( $param );
	orderPush( function():void{
		var call:Function;
		call = arguments.callee;
		if( treeCheck( $key ) ){
			TAFloader.LOAD( 'ini', $path, param, function( $data:Object ):void{
				addTree( '@DATA', $key, 'ini', $data, param );
				orderCollect( call );
				poolCollectParam( param );
			} );
		}else{
			orderCollect( call );
			poolCollectParam( param );
		}
	} );
}
final public function addImage( $key:String, $path:String, ...$param ):void{
	var param:TAFparam;
	param = $paramGet( $param );
	orderPush( function():void{
		var call:Function;
		call = arguments.callee;
		if( treeCheck( $key ) ){
			TAFloader.LOAD( 'image', $path, param, function( $data:BitmapData ):void{
				addTree( '@DATA', $key, 'image', $data, param );
				addTree( '@DATA', $key+'@size', 'data', [$data.width, $data.height], null );
				orderCollect( call );
				poolCollectParam( param );
			} );
		}else{
			orderCollect( call );
			poolCollectParam( param );
		}
	} );
}
final public function addTile( $key:String, $path:String, $cellX:int, $cellY:int, ...$param ):void{
	var param:TAFparam;
	param = $paramGet( $param );
	orderPush( function():void{
		var call:Function;
		call = arguments.callee;
		if( treeCheck( $key ) ){
			TAFloader.LOAD( 'image', $path, param, function( $data:BitmapData ):void{
				addTileEnd( $key, $data, $cellX, $cellY, param );
				orderCollect( call );
				poolCollectParam( param );
			} );
		}else{
			orderCollect( call );
			poolCollectParam( param );
		}
	} );
}
final public function addTileKey( $key:String, $resKey:String, $cellX:int, $cellY:int, ...$param ):void{
	var param:TAFparam;
	param = $paramGet( $param );
	orderAddByKeyCheck( $key, param, function():void{
		addTileEnd( $key, tdata( $resKey ), $cellX, $cellY, param );
	} );
}
private function addTileEnd( $key:String, $data:BitmapData, $cellX:int, $cellY:int, $param:TAFparam ):void{
	var temp:Array, i:int, j:int;
	temp = bitmapTile( $cellX, $cellY, $data );
	for( i = 0, j = temp.length ; i < j ; ++i ){
		addTree( '@DATA', $key + i.toString(), 'image', temp[i], $param );
		addTree( '@DATA', $key + i.toString() +'@size', 'data', [temp[i].width, temp[i].height], null );
	}
}
final public function addMap( $path:String, $map:Object, ...$param ):void{
	var param:TAFparam;
	param = $paramGet( $param );
	orderPush( function():void{
		var call:Function;
		call = arguments.callee;
		TAFloader.LOAD( 'image', $path, param, function( $data:BitmapData ):void{
			addMapEnd( $data, $map, param );
			orderCollect( call );
			poolCollectParam( param );
		} );
	} );
}
private function addMapEnd( $data:BitmapData, $map:Object, $param:TAFparam = null ):void{
	var temp:Object, key:*;
	temp = bitmapMap( $map, $data );
	for( key in temp ){
		addTree( '@DATA', key, 'image', temp[key], $param );
		addTree( '@DATA', key + '@size', 'data', [temp[key].width, temp[key].height], null );
	}
}
//-----------------------------------------------------------------------------------------------------
final public function addData( $key:String, $data:*, ...$param ):void{
	var param:TAFparam;
	param = $paramGet( $param );
	orderAddByKeyCheck( $key, param, function():void{
		addTree( '@DATA', $key, 'data', $data, param );
	} );
}
final public function addBitmapData( $key:String, $data:BitmapData, ...$param ):void{
	var param:TAFparam;
	param = $paramGet( $param );
	orderAddByKeyCheck( $key, param, function():void{
		addTree( '@DATA', $key, 'image', $data, param );
		addTree( '@DATA', $key + '@size', 'data', [$data.width, $data.height], null );
	} );
}
final public function addByte( $key:String, $path:String, ...$param ):void{
	var param:TAFparam;
	param = $paramGet( $param );
	orderPush( function():void{
		var call:Function;
		call = arguments.callee;
		if( treeCheck( $key ) ){
			TAFloader.LOAD( 'byte', $path, param, function( $data:ByteArray ):void{
				addTree( '@DATA', $key, 'byte', $data, param );
				orderCollect( call );
				poolCollectParam( param );
			} );
		}else{
			orderCollect( call );
			poolCollectParam( param );
		}
	} );
}
final public function addText( $key:String, $path:String, ...$param ):void{
	var param:TAFparam;
	param = $paramGet( $param );
	orderPush( function():void{
		var call:Function;
		call = arguments.callee;
		if( treeCheck( $key ) ){
			TAFloader.LOAD( 'text', $path, param, function( $data:Object ):void{
				addTree( '@DATA', $key, 'text', $data, param );
				orderCollect( call );
				poolCollectParam( param );
			} );
		}else{
			orderCollect( call );
			poolCollectParam( param );
		}
	} );
}
//-----------------------------------------------------------------------------------------------------
final public function addSound( $key:String, $path:String, ...$param ):void{
	var param:TAFparam;
	param = $paramGet( $param );
	orderPush( function():void{
		var call:Function;
		call = arguments.callee;
		if( treeCheck( $key ) ){
			TAFloader.LOAD( 'byte', $path, param, function( $data:ByteArray ):void{
				byteToSound( $data, function( $sound:Sound ):void{
					addTree( '@DATA', $key, 'sound', poolPullSound( $sound ), param );
					orderCollect( call );
					poolCollectParam( param );
				} );
			} );
		}else{
			orderCollect( call );
			poolCollectParam( param );
		}
	} );
}